package com.example.myapplication8_5;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class FruitAdapter extends BaseAdapter {

    private Context context;
    private List<Fruit> fruitList;

    public FruitAdapter(Context context, List<Fruit> fruitList) {
        this.context = context;
        this.fruitList = fruitList;
    }

    @Override
    public int getCount() {
        return fruitList.size();
    }

    @Override
    public Object getItem(int position) {
        return fruitList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fruit_item, parent, false);
        }

        ImageView fruitImage = convertView.findViewById(R.id.fruit_image);
        TextView fruitName = convertView.findViewById(R.id.fruit_name);

        Fruit fruit = fruitList.get(position);
        fruitImage.setImageResource(fruit.getImageResId());
        fruitName.setText(fruit.getName());

        return convertView;
    }
}